package demo.mq.rocket;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import demo.mq.rocket.vo.GroupInfo;
import demo.mq.rocket.vo.GroupStatInfo;
import demo.mq.rocket.vo.TopicQueueStats;

public class RocketMqMonitor {

    private static Logger logger = LoggerFactory.getLogger(RocketMqMonitor.class);

    public static String url = "http://192.168.60.40:8080";
    public static String user = "admin";
    public static String pass = "admin";

    public static RocketMqConsoleApi client = new RocketMqConsoleApi(url, user, pass);
    static {
        try {
            String sessionId = client.logginGetSessionId();
            logger.info("sessionId = {}", sessionId);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Test
    public void queryTopicByConsumer() {
        String consumerGroup = "workflow_consumer";
        try {
            List<GroupStatInfo> list = client.queryTopicByConsumer(consumerGroup);
            list.forEach(System.out::println);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Test
    public void groupList() {
        try {
            List<GroupInfo> list = client.groupList();
            list.forEach(System.out::println);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Test
    public void queryConsumerByTopic() {
        String topic = "business_order_repairXXXXXXX";
        try {
            JSONObject json = client.queryConsumerByTopic(topic);
            logger.info("{}", json);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            List<GroupStatInfo> list = client.queryConsumerByTopicAsList(topic);
            list.forEach(System.out::println);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Test
    public void topicStats() {
        String topic = "BasicUseTopic";
        try {
            JSONObject json = client.topicStats(topic);
            logger.info("{}", json);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            List<TopicQueueStats> list = client.topicStatsAsList(topic);
            list.forEach(System.out::println);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Test
    public void topicListGroupByType() {
        try {
            Map<String, List<String>> map = client.topicListGroupByType();
            logger.info("{}", map);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Test
    public void topicList() {
        List<String> topicList = null;
        try {
            topicList = client.topicList();
            logger.info("response = {}", topicList);
        } catch (Exception e) {
            logger.error("", e);
        }
        if (topicList == null) {
            return;
        }
        topicList.stream().forEach(e -> logger.info(e.toString()));
    }

    @Test
    public void topicCurrent() {
        List<String> currentTopics = null;
        try {
            currentTopics = client.topicCurrent();
            logger.info("response = {}", currentTopics);
        } catch (Exception e) {
            logger.error("", e);
        }
        if (currentTopics == null) {
            return;
        }
        currentTopics.stream().forEach(e -> logger.info(e.toString()));
    }

    @Test
    public void login() {
        try {
            JSONObject response = client.loggin();
            logger.info("response = {}", response);
        } catch (Exception e) {
            logger.error("", e);
        }
    }

}
